package com.smartwasser.yunhtgl.apphtgl.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;

import com.dou361.dialogui.DialogAssigner;
import com.dou361.dialogui.config.BuildBean;
import com.smartwasser.yunhtgl.apphtgl.App;

import static com.smartwasser.yunhtgl.apphtgl.base.BaseConstant.error;
import static com.smartwasser.yunhtgl.apphtgl.widget.CustomToast.showToast;

public abstract class BaseActivity extends Activity implements View.OnClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        App.getInstance().addActivity(this);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentLayout();
        initView();
        initData();
    }

    public abstract void setContentLayout();

    public abstract void initView();

    public abstract void initData();

    public void startActivity(Class<?> cls) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
    }

    public void startActivity(Class<?> cls, String key, String value) {
        Intent intent = new Intent(this, cls);
        intent.putExtra(key, value);
        startActivity(intent);
    }

    public void startActivity(Class<?> cls, String key, Bundle value) {
        Intent intent = new Intent(this, cls);
        intent.putExtra(key, value);
        startActivity(intent);
    }

    public void showResult(final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(getApplication(), msg);
            }
        });
    }

    public static BuildBean showLoadingHorizontal(Context context, CharSequence msg) {
        return showLoadingHorizontal(context, msg, true, true, true);
    }

    public static BuildBean showLoadingHorizontal(Context context, CharSequence msg, boolean cancleable, boolean outsideTouchable, boolean isWhiteBg) {
        return DialogAssigner.getInstance().assignLoadingHorizontal(context, msg, cancleable, outsideTouchable, isWhiteBg);
    }

    protected void onDestroy() {
        super.onDestroy();
        App.getInstance().removeActivity(this);
    }

    public void showError() {
        showResult(error);
    }
}